import request from '@/utils/request'

// 用户信息
export interface User {
  id: number
  username: string
  name: string
  email: string
  status: number
  created_at: string
  updated_at: string
  roles: Array<{
    id: number
    name: string
  }>
}

// 创建用户请求
export interface CreateUserRequest {
  username: string
  password: string
  name: string
  email?: string
  status: number
  role_ids: number[]
}

// 更新用户请求
export interface UpdateUserRequest {
  name: string
  email?: string
  status: number
  role_ids: number[]
}

// 用户列表响应
export interface UserListResponse {
  list: User[]
  total: number
  page: number
  page_size: number
}

// 重置密码请求
export interface ResetPasswordRequest {
  new_password: string
}

/**
 * 获取用户列表
 */
export function getUserList(params: {
  page?: number
  page_size?: number
  keyword?: string
}): Promise<UserListResponse> {
  return request.get('/users', { params })
}

/**
 * 获取用户详情
 */
export function getUserDetail(id: number): Promise<User> {
  return request.get(`/users/${id}`)
}

/**
 * 创建用户
 */
export function createUser(data: CreateUserRequest): Promise<User> {
  return request.post('/users', data)
}

/**
 * 更新用户
 */
export function updateUser(id: number, data: UpdateUserRequest): Promise<{ message: string }> {
  return request.put(`/users/${id}`, data)
}

/**
 * 删除用户
 */
export function deleteUser(id: number): Promise<{ message: string }> {
  return request.delete(`/users/${id}`)
}

/**
 * 重置用户密码
 */
export function resetUserPassword(id: number, data: ResetPasswordRequest): Promise<{ message: string }> {
  return request.post(`/users/${id}/reset-password`, data)
}
